package com.ycomputer.dialogactivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.ycomputer.pizzashow.R;
import com.ycomputer.utils.StringUtils;

public class SetPasswordActivity extends BaseDialogActivity implements View.OnClickListener{

	private String password;
	private String passwordConfirm;
	private int type;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_password_activity);
		type = bundle.getInt("type", 0);
		switch (type) {
		case 1://输入密码对话框
			findViewById(R.id.setpassword_input_container).setVisibility(View.GONE);
			break;
		default:
			break;
		}
		findViewById(R.id.setpassword_ok).setOnClickListener(this);
		findViewById(R.id.setpassword_cancel).setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.set_password, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setpassword_ok:
			password = ((EditText)findViewById(R.id.setpassword_input)).getText().toString().trim();
			passwordConfirm = ((EditText)findViewById(R.id.setpassword_confirm)).getText().toString().trim();
			if(type == 0){
				if(StringUtils.isEmptyString(password)){
					Toast.makeText(this, R.string.setpassword_input_warning1, Toast.LENGTH_SHORT).show();
					return;
				}
				if(StringUtils.isEmptyString(passwordConfirm)){
					Toast.makeText(this, R.string.setpassword_input_warning1, Toast.LENGTH_SHORT).show();
					return;
				}
				if(!password.equals(passwordConfirm)){
					Toast.makeText(this, R.string.setpassword_input_warning2, Toast.LENGTH_SHORT).show();
					return;
				}
				savePassword();
				finish();
			}else if(type == 0){//确认密码对话框
				matchPassword();
			}
			break;
			
		case R.id.setpassword_cancel:
			finish();
			break;

		default:
			break;
		}
		
	}

	private void matchPassword() {
		SharedPreferences passwordPreferences= this.getSharedPreferences("passwordPreferences", Context.MODE_PRIVATE);
		String savePassword = passwordPreferences.getString("password", "");
		if(!StringUtils.isEmptyString(savePassword) && !StringUtils.isEmptyString(password)){
			if(password.equals(savePassword)){
				//管理员模式
				setResult(0);
				finish();
			}else{
				Toast.makeText(this, R.string.setpassword_input_warning2, Toast.LENGTH_SHORT).show();
			}
		}
	}

	private void savePassword() {
		SharedPreferences passwordPreferences= this.getSharedPreferences("passwordPreferences", Context.MODE_PRIVATE);
		Editor editor = passwordPreferences.edit();
		editor.putString("password", password);
		editor.commit();
	}

}
